﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace com.zhongjh.Library.Configuration
{
    /// <summary>
    /// 配置项
    /// </summary>
    public class ConfigItem
    {
        /// <summary>
        /// 键
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        public string Value { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }
        /// <summary>
        /// 子节点
        /// </summary>
        public List<ConfigItem> ChildItems { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ConfigItem()
        {
            Key = string.Empty;
            Value = string.Empty;
            Remark = string.Empty;
            ChildItems = new List<ConfigItem>();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="currNode">XML节点</param>
        internal ConfigItem(XmlNode currNode)
            : this()
        {
            if (currNode.Attributes["key"] != null)
                this.Key = currNode.Attributes["key"].Value;
            if (currNode.Attributes["value"] != null)
                this.Value = currNode.Attributes["value"].Value;
            if (currNode.Attributes["remark"] != null)
                this.Remark = currNode.Attributes["remark"].Value;

            //添加项的子节点
            foreach (XmlNode temp in currNode.ChildNodes)
            {
                ConfigItem item = new ConfigItem(temp);
                this.ChildItems.Add(item);
            }
        }

        /// <summary>
        /// 获取项(包括自己)
        /// </summary>
        /// <param name="key">项</param>
        /// <returns>配置项</returns>
        internal ConfigItem GetItem(string key)
        {
            if (string.IsNullOrEmpty(key))
                return null;

            ConfigItem result = null;
            if (this.Key == key)
            {
                result = this;
            }
            else
            {
                foreach (ConfigItem temp in this.ChildItems)
                {
                    result = temp.GetItem(key);
                    if (result != null)
                        break;
                }
            }
            return result;
        }

        /// <summary>
        /// 获取子项
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        internal ConfigItem GetChildItem(string key)
        {
            if (string.IsNullOrEmpty(key))
                return null;

            ConfigItem result = null;
            foreach (ConfigItem temp in this.ChildItems)
            {
                if (temp.Key == key)
                {
                    result = temp.GetItem(key);
                    break;
                }
            }
            return result;
        }
    }
}
